package com.seek.tuana.lab;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekbarwithAnimationActivity extends Activity{
    private SeekBar sek1;
	private SeekBar sek2;
	private SeekBar sek3;
	private static ImageView im1;
	private ImageView im2;
	private ImageView im3;
	private AnimationDrawable animationm1;
	private Runnable mrun1;
	private boolean FLAG_RUN1=true;
	private int time1=1;
	private Thread th1;
	private Runnable mrun2;
	private boolean FLAG_RUN2 = true;
	private int time2 = 1;
	private Thread th2;
	private AnimationDrawable animationm2;
	private Runnable mrun3;
	private AnimationDrawable animationm3;
	private boolean FLAG_RUN3 = true;
	private int time3 = 1;
	private Thread th3;
	

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sek1 = (SeekBar)findViewById(R.id.SeekBar01);
        sek2 = (SeekBar)findViewById(R.id.SeekBar02);
        sek3 = (SeekBar)findViewById(R.id.seekBar3);
        
        im1 =(ImageView)findViewById(R.id.ImageView01);
        im2 =(ImageView)findViewById(R.id.ImageView02);
        im3 =(ImageView)findViewById(R.id.imageView3);
        
        
        //Lấy tài nguyên dạng AnimationDrawable
        im1.setBackgroundResource(R.drawable.fileimage);
        animationm1 =(AnimationDrawable)im1.getBackground();
        
        animationm2 =(AnimationDrawable)im1.getBackground();
        animationm3 =(AnimationDrawable)im1.getBackground();
        
        mrun1 = new Runnable() {
        	
			public void run() {
				int totalFrame = animationm1.getNumberOfFrames();
				int count = 0;
				while(FLAG_RUN1){
					try {
						if(count>totalFrame)
							count = 0;
						UpdateAni(count,1);
						Thread.sleep(time1*10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count++;
				}
			}
		};
        th1 = new Thread(mrun1);
        th1.start();
        
		mrun2 = new Runnable() {

			public void run() {
				int totalFrame = animationm1.getNumberOfFrames();
				int count = 0;
				while(FLAG_RUN2){
					try {
						if(count>totalFrame)
							count = 0;
						UpdateAni(count,2);
						Thread.sleep(time2 *10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count++;
				}
			}

			
		};
		th2 = new Thread(mrun2);
		th2.start();
		
		mrun3 = new Runnable() {


			public void run() {
				int totalFrame = animationm3.getNumberOfFrames();
				int count = 0;
				while(FLAG_RUN3){
					try {
						if(count>totalFrame)
							count = 0;
						UpdateAni(count,3);
						Thread.sleep(time3  *10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count++;
				}
			}

			
		};
		th3 = new Thread(mrun3);
		th3.start();
		sek1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar see, int process, boolean fromuser) {
				time1 = process;
			}
		});
		
        
        
        sek2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				time2 = progress;
			}
		});
        sek3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				time3 = progress;
			}
		});
        
		
	}
    public  void UpdateAni(final int count, final int Flag){
//    	runOnUiThread(im1.setBackgroundDrawable(drawable));
    	runOnUiThread(new Runnable() {
			
			public void run() {
				
				if(Flag == 1)
					im1.setBackgroundDrawable(animationm1.getFrame(count));
				if(Flag == 2)
					im2.setBackgroundDrawable(animationm2.getFrame(count));
				if(Flag == 3)
					im3.setBackgroundDrawable(animationm3.getFrame(count));
			}
		});
    }
	
}